package com.demo.connectdbtest.module.exportrecord.controller;

import com.demo.connectdbtest.module.common.constants.CommonBizStatus;
import com.demo.connectdbtest.module.common.util.ExceptionUtil;
import com.demo.connectdbtest.module.exportrecord.service.ExportService;
import com.demo.connectdbtest.module.rest.response.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;


/**
 * 描述：
 *
 * @author: chengc
 * @date 2022/03/28
 */
@RestController
@RequestMapping("/api/data")
@Api(tags = "导出相关接口")
public class ExportController {

    private static final Logger log = LoggerFactory.getLogger(ExportController.class);

    @Autowired
    private ExportService exportService;

   /* @GetMapping("/export/list")
    @ApiOperation(value = "导出数据列表")
    public APiResponsePage<ExportResp, Long> exportList(ListExportReqParams req) {
        PageInfo<ExportResp> resp = null;
        try {
            resp = exportService.findExportDataList(req);
        } catch (Exception e) {
            log.error("獲取导出数据列表error={}", ExceptionUtil.getStackMsg(e));
            Asserts.fail("查詢失敗");
        }
        return APiResponsePage.ok(resp);
    }

    @ApiOperation(value = "下载导出数据", httpMethod = "POST")
    @PostMapping("/export/record")
    public void exportRecord(@RequestBody ExportReq req, HttpServletResponse resp) {
        exportService.exportData(req, resp);
    }


    @ApiOperation(value = "删除导出下载记录")
    @GetMapping("/export/record/delete")
    public ApiResponse deleteExportRecordById(@RequestParam("id") Long id) {
        try {
            exportService.deleteExportRecordById(id);
        } catch (Exception e) {
            log.error("删除导出下载记录error={}", ExceptionUtil.getStackMsg(e));
            Asserts.fail(e.getMessage());
        }
        return ApiResponse.ok();
    }*/


    @ApiOperation(value = "导出不同时间点的直播人数")
    @GetMapping("/live/num/export")
    public ApiResponse exportLiveNum(@RequestParam("startTime") Long startTime, @RequestParam("endTime") Long endTime, HttpServletResponse response) {
        try {
            exportService.exportLiveNum(startTime, endTime, response);
        } catch (Exception e) {
            log.error("导出不同时间点的直播人数error={}", ExceptionUtil.getStackMsg(e));
            return ApiResponse.err(CommonBizStatus.F_ERROR);
        }
        return ApiResponse.ok();
    }
}
